package com.ah.shanxi_demo.convert;

import com.ah.shanxi_demo.common.enums.PolicyCommonEnum;
import com.ah.shanxi_demo.param.input.PolicyCollectRO;
import com.ah.shanxi_demo.param.result.PolicyDataReportMapperPO;
import com.ah.shanxi_demo.sync.model.*;
import com.ah.shanxi_demo.sync.param.LogList;
import com.ah.shanxi_demo.sync.param.PolicyDataReportPO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 合并组装部级平台策略采集数据接口传参
 * @author zt 2020/8/27
 */
public class ConvertPolicyData {


    public static PolicyDataReportPO merge_convert_policy_data_ro(PolicyDataReportMapperPO dataReportMapperPO, PolicyCollectRO policyCollectRO){

        PolicyDataReportPO resultModel=new PolicyDataReportPO();
        resultModel.setCenter_code(dataReportMapperPO.getCenter_code());
        resultModel.setCenter_key(dataReportMapperPO.getCenter_key());
        resultModel.setPolicy_code(dataReportMapperPO.getPolicy_code());

        List<LogList> logList=new ArrayList<>();

        if (StringUtils.isBlank(policyCollectRO.getSyslog())){
            resultModel.setLog_list(logList);
            return  resultModel;
        }

        //确定策略上传的是哪种类型的策略
        PolicyCommonEnum policyCommonEnum = PolicyCommonEnum.convert(dataReportMapperPO.getPolicy_type());

        //用特定的策略模板 对接受的数据进行转换
        switch (policyCommonEnum) {
            case POLICY_TYPE_1:
                logList = convert_policy_data_type_1(dataReportMapperPO, policyCollectRO.getSyslog());
                break;
            case POLICY_TYPE_2:
                logList = convert_policy_data_type_1(dataReportMapperPO, policyCollectRO.getSyslog());

                break;
            case POLICY_TYPE_3:
                logList= convert_policy_data_type_1(dataReportMapperPO, policyCollectRO.getSyslog());

                break;
            case POLICY_TYPE_4:
                logList = convert_policy_data_type_1(dataReportMapperPO, policyCollectRO.getSyslog());

                break;
            case POLICY_TYPE_5:
                logList= convert_policy_data_type_1(dataReportMapperPO, policyCollectRO.getSyslog());

                break;
            default:
                break;
        }
        //组装成为部级平台策略数据采集接口所需要的参数
        resultModel.setLog_list(logList);

        return resultModel;
    }



    public static List<LogList> convert_policy_data_type_1(PolicyDataReportMapperPO dataReportMapperPO, String sysLogString){

        List<LogList> logList=new ArrayList<>();

        List<ALM011> alm011List = JSONObject.parseArray(sysLogString, ALM011.class);

        if (CollectionUtils.isEmpty(alm011List)){
            return logList;
        }

        alm011List.stream().forEach(alm011 -> {

            LogList logModel=new LogList();
            logModel.setSyslog(JSON.toJSONString(alm011));
            logModel.setDevice_guid(dataReportMapperPO.getDevice_guid());
            logModel.setLog_type(dataReportMapperPO.getLog_type());
            logModel.setPolicy_type(dataReportMapperPO.getPolicy_type());
//            logModel.setLog_id();

            logModel.setLast_result_flag(0);

            logList.add(logModel);
        });

        return logList;
    }

    public List<LogList> convert_policy_data_type_2(PolicyDataReportMapperPO dataReportMapperPO,String sysLogString){

        List<LogList> logList=new ArrayList<>();

        List<ALM012> alm011List = JSONObject.parseArray(sysLogString, ALM012.class);

        if (CollectionUtils.isEmpty(alm011List)){
            return logList;
        }

        alm011List.stream().forEach(alm012 -> {

            LogList logModel=new LogList();
            logModel.setSyslog(JSON.toJSONString(alm012));
            logModel.setDevice_guid(dataReportMapperPO.getDevice_guid());
            logModel.setLog_type(dataReportMapperPO.getLog_type());
            logModel.setPolicy_type(dataReportMapperPO.getPolicy_type());
//            logModel.setLog_id();

            logModel.setLast_result_flag(0);

            logList.add(logModel);
        });

        return logList;
    }

    public List<LogList> convert_policy_data_type_3(PolicyDataReportMapperPO dataReportMapperPO,String sysLogString){

        List<LogList> logList=new ArrayList<>();

        List<ALM013> alm011List = JSONObject.parseArray(sysLogString, ALM013.class);

        if (CollectionUtils.isEmpty(alm011List)){
            return logList;
        }

        alm011List.stream().forEach(alm013 -> {

            LogList logModel=new LogList();
            logModel.setSyslog(JSON.toJSONString(alm013));
            logModel.setDevice_guid(dataReportMapperPO.getDevice_guid());
            logModel.setLog_type(dataReportMapperPO.getLog_type());
            logModel.setPolicy_type(dataReportMapperPO.getPolicy_type());
//            logModel.setLog_id();

            logModel.setLast_result_flag(0);

            logList.add(logModel);
        });

        return logList;
    }

    public List<LogList> convert_policy_data_type_4(PolicyDataReportMapperPO dataReportMapperPO,String sysLogString){

        List<LogList> logList=new ArrayList<>();

        List<ALM014> alm011List = JSONObject.parseArray(sysLogString, ALM014.class);

        if (CollectionUtils.isEmpty(alm011List)){
            return logList;
        }

        alm011List.stream().forEach(alm014 -> {

            LogList logModel=new LogList();
            logModel.setSyslog(JSON.toJSONString(alm014));
            logModel.setDevice_guid(dataReportMapperPO.getDevice_guid());
            logModel.setLog_type(dataReportMapperPO.getLog_type());
            logModel.setPolicy_type(dataReportMapperPO.getPolicy_type());
//            logModel.setLog_id();

            logModel.setLast_result_flag(0);

            logList.add(logModel);
        });

        return logList;
    }

    public List<LogList> convert_policy_data_type_5(PolicyDataReportMapperPO dataReportMapperPO,String sysLogString){

        List<LogList> logList=new ArrayList<>();

        List<ALM015> alm011List = JSONObject.parseArray(sysLogString, ALM015.class);

        if (CollectionUtils.isEmpty(alm011List)){
            return logList;
        }

        alm011List.stream().forEach(alm015 -> {

            LogList logModel=new LogList();
            logModel.setSyslog(JSON.toJSONString(alm015));
            logModel.setDevice_guid(dataReportMapperPO.getDevice_guid());
            logModel.setLog_type(dataReportMapperPO.getLog_type());
            logModel.setPolicy_type(dataReportMapperPO.getPolicy_type());
//            logModel.setLog_id();

            logModel.setLast_result_flag(0);

            logList.add(logModel);
        });

        return logList;
    }








}
